package com.itcanteen.medical.docker.service.impl;

import com.itcanteen.medical.common.constant.Contstant;
import com.itcanteen.medical.common.constant.Status;
import com.itcanteen.medical.common.exception.AdException;
import com.itcanteen.medical.docker.entity.Doctor;
import com.itcanteen.medical.docker.entity.Level;
import com.itcanteen.medical.docker.entity.Patient;
import com.itcanteen.medical.docker.entity.RegistrationRecording;
import com.itcanteen.medical.docker.mapper.DoctorMapper;
import com.itcanteen.medical.docker.mapper.LevelMapper;
import com.itcanteen.medical.docker.mapper.PatientMapper;
import com.itcanteen.medical.docker.mapper.RegistrationRecordingMapper;
import com.itcanteen.medical.docker.service.RegistrationRecordingService;
import com.itcanteen.medical.docker.unified.management.RequestParameter;
import com.itcanteen.medical.docker.util.SendSms;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class RegistrationRecordingServiceImpl implements RegistrationRecordingService {

    @Autowired
    private RegistrationRecordingMapper mapper;
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private LevelMapper levelMapper;
    @Autowired
    private PatientMapper patientMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 每天执行一次，定期查看
     */
    //@Scheduled(cron = "0 */1 * * * ?")
    public void isOverdue() {
        //获取全部数据
        List<RegistrationRecording> list = mapper.selectAll();
        Date date = new Date();//当前时间
        System.out.println(date);
        for (RegistrationRecording recording : list) {

            if (date.after(recording.getCreateTime())) {

                if (!Status.FAIL.equals(recording.getStatus())) {

                    recording.setStatus(Status.FAIL.getCode());

                    SendSms.sendMag("15204178957", "123");
                    mapper.updateByPrimaryKey(recording);
                }
            }
        }

    }

    /**
     * 取消挂号
     * @param requestParameter
     * @return CommonResponse
     */
    @Override
    public RegistrationRecording cancel(RequestParameter requestParameter) throws AdException {
        //校验为空
        if(requestParameter.validate()){
            throw  new AdException(Contstant.ErrorMsg.REQUEST_PARAM_ERROR);
        }

        RegistrationRecording recording = mapper.selectByPrimaryKey(requestParameter.getId());

        if (recording!=null && ! Status.FAIL.getCode().equals(recording.getStatus())){
            //获取退款金额
            Double refund = this.refund(recording.getDoctorId());

            Patient patient = patientMapper.selectByPrimaryKey(recording.getPatientId());
            //退款
            patient.setBalance(patient.getBalance()+refund);
            int i = patientMapper.updateByPrimaryKey(patient);
            //设置状态为取消状态
            recording.setStatus(Status.CANCEL.getCode());
            mapper.updateByPrimaryKey(recording);

            return recording;
        }else{
            throw new AdException(Contstant.ErrorMsg.NOTNULL);
        }


    }

    //获取退款金额
    private Double refund(Long id) throws AdException {
        Doctor doctor = doctorMapper.selectByPrimaryKey(id);
        if (doctor == null){
            throw new AdException(Contstant.ErrorMsg.NOTNULL);
        }

        Level level = levelMapper.selectByPrimaryKey(doctor.getLevelId());

        if (level == null){
            throw new AdException(Contstant.ErrorMsg.NOTNULL);
        }
        return level.getPrice();
    }


}
